﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace ConnectedWebParts.DateAndTime
{
    class DateFormatEditorPart : EditorPart
    {
        public DropDownList Formats { get; set; }

        protected override void CreateChildControls()
        {
            Formats = new DropDownList();
            Formats.Items.Add("dd/MM/yyyy");
            Formats.Items.Add("MM/dd/yyyy");
            Formats.Items.Add("dd/MMM/yyyy");
            Formats.Items.Add("dd/MM/yy");
            Formats.Items.Add("dd-MMM-yyyyy");

            Controls.Add(new LiteralControl("Format de la date : "));
            Controls.Add(Formats);
        }

        #region implement abstract method
        // those two method are necessary to persist the data
        public override bool ApplyChanges()
        {
            EnsureChildControls();
            DateAndTime webpart = (DateAndTime)this.WebPartToEdit;
            webpart.DateFormat = Formats.SelectedValue;
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            DateAndTime webpart = (DateAndTime)this.WebPartToEdit;
            if(webpart.DateFormat != null) Formats.Items.FindByText(webpart.DateFormat).Selected = true;
        }
        #endregion
    }
}
